package com.afinal.intelligencetrash.Presenter;



import com.afinal.intelligencetrash.Model.bean.User;
import com.afinal.intelligencetrash.Model.db.LocalUserDB;
import com.afinal.intelligencetrash.Model.interfaces.LoginModel;
import com.afinal.intelligencetrash.Model.interfaces.RequestHolder;
import com.afinal.intelligencetrash.Presenter.Interinfaces.ActionCallBack;
import com.afinal.intelligencetrash.Presenter.Interinfaces.LoginService;
import com.afinal.intelligencetrash.global.GlobalEnvironment;
import com.afinal.intelligencetrash.ui.interfaces.LoginView;

import javax.inject.Inject;

/**
 * Created by cyy on 17-10-8.
 */

public class LoginServiceImp implements LoginService {

    private LoginModel model;
    private LoginView view;

    @Inject
    public LoginServiceImp(LoginView view) {
        this.view = view;
        model = LocalUserDB.getInstance();
    }

    @Override
    public void login(String username, String pwd, final ActionCallBack callBack) {
        if(callBack != null){
            callBack.before();
        }
        model.login(username, pwd, new RequestHolder<User>() {
            @Override
            public void OnSuccess(User result) {
                GlobalEnvironment.currentUser = result;
            }

            @Override
            public void OnFailure(int statusCode, String error) {
                view.showToast(error);
            }

            @Override
            public void OnFinal(boolean isSuccess) {
                if(callBack != null){
                    callBack.after(isSuccess);
                }
            }
        });

    }
}
